package com.foreverwin.mes.lims.service.impl;

import com.foreverwin.mes.common.enums.IDUtils;
import com.foreverwin.mes.core.base.FrontPage;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.foreverwin.mes.lims.mapper.ItemTankMapper;
import com.foreverwin.mes.lims.model.DelayInactive;
import com.foreverwin.mes.lims.mapper.DelayInactiveMapper;
import com.foreverwin.mes.lims.model.ItemTank;
import com.foreverwin.mes.lims.service.DelayInactiveService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 延长呆滞时间 服务实现类
 * </p>
 *
 * @author Max
 * @since 2020-03-11
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class DelayInactiveServiceImpl extends ServiceImpl<DelayInactiveMapper, DelayInactive> implements DelayInactiveService {


    @Autowired
    private DelayInactiveMapper delayInactiveMapper;
    @Autowired
    private ItemTankMapper itemTankMapper;

    @Override
    public Page<DelayInactive> selectPage(FrontPage<DelayInactive> frontPage, DelayInactive delayInactive) {
        EntityWrapper<DelayInactive> queryWrapper = new EntityWrapper<>();
        queryWrapper.setEntity(delayInactive);
        return super.selectPage(frontPage.getPagePlus(), queryWrapper);
    }

    @Override
    public List<DelayInactive> selectList(DelayInactive delayInactive) {
        EntityWrapper<DelayInactive> queryWrapper = new EntityWrapper<>();
        queryWrapper.setEntity(delayInactive);
        return super.selectList(queryWrapper);
    }

    /**
     * 延长呆滞时间
     *
     * @param delayInactive
     * @return
     */
    @Override
    public int delayActiveTime(DelayInactive delayInactive,String user) {
        delayInactive.setHandle( UUID.randomUUID().toString() );
        delayInactive.setCreator( user );
        delayInactive.setCreatedDateTime( new Date() );
        delayInactive.setModifiedDateTime( new Date() );
        int conut = delayInactiveMapper.insert(delayInactive);

        ItemTank itemTank = itemTankMapper.selectById(delayInactive.getzItemTankBo());
        itemTank.setReleaseNum( 1);
        //增加预计上限时间
        if ( itemTank.getEstimateUpperTime() != null ) {
            long time = itemTank.getEstimateUpperTime().getTime() + delayInactive.getDelayTime()*60L*60L*1000L;
            Date date = new Date( time );
            itemTank.setEstimateUpperTime( date );
        }
        itemTank.setTotalDelayTime( new BigDecimal( delayInactive.getDelayTime() ));
        itemTankMapper.updateDelayOfItemTank( itemTank);
        return conut;
    }


}